package com.ruoyi.app.controller;

import com.ruoyi.app.service.AppEquipmentManagementService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.FtpConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.JSONObjectPro;
import com.ruoyi.common.core.domain.LabelValue;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.comlbs.ObjectTypeEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.system.domain.DriverInfo;
import com.ruoyi.system.domain.VehicleManagement;
import com.ruoyi.system.domain.request.vehicle.NumberPlateExistParam;
import com.ruoyi.system.domain.vo.equipment.EquipmentInfo;
import com.ruoyi.system.service.IDriverInfoService;
import com.ruoyi.system.service.IEquipmentManagementService;
import com.ruoyi.system.service.ISimCardService;
import com.ruoyi.system.service.IVehicleManagementService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 描述
 * 作者 ZhangPing
 * 创建时间 2023/10/9 10:35
 */
@RestController
@RequestMapping("/app/equipmentManagement")
@Slf4j
public class AppEquipmentManagementController extends BaseController {

    @Autowired
    private AppEquipmentManagementService appEquipmentManagementService;
    @Autowired
    private IEquipmentManagementService equipmentManagementService;
    @Autowired
    private ISimCardService simCardService;
    @Autowired
    private IVehicleManagementService vehicleManagementService;
    @Autowired
    private IDriverInfoService driverInfoService;
    @Autowired
    private FtpConfig ftpConfig;

    @ApiOperation("设备号重复校验")
    @PostMapping("/exist")
    public AjaxResult exist(@RequestParam String deviceId) {
        return success(appEquipmentManagementService.checkExistDeviceId(deviceId));
    }

    @ApiOperation("设备型号列表")
    @GetMapping("/deviceModelList")
    public AjaxResult deviceModelList() {
        List<SysDictData> dataList = DictUtils.getDictCache("deviceModel");
        return success(dataList.stream().map(i -> new LabelValue(i.getDictLabel(), i.getDictValue())).collect(Collectors.toList()));
    }

    @ApiOperation("设备管理添加sim卡号时验证")
    @PostMapping("/getSimCardInfo")
    public AjaxResult getSimCardInfo(@RequestParam String iccid, @RequestParam Long id) {
        equipmentManagementService.checkExistSimId(iccid, id);
        if (simCardService.getSimCardInfo(iccid) == null) throw new ServiceException("SIM卡不存在");
        return success();
    }

    @ApiOperation("监管对象下拉列表")
    @PostMapping("/vehicleSelectList")
    public AjaxResult vehicleSelectList(@RequestParam String numberPlate, @RequestParam Integer objType, @RequestParam Long companyId) {
        List<VehicleManagement> vehicleList = vehicleManagementService.selectList(new VehicleManagement()
                .setNumberPlate(numberPlate).setObjType(objType).setCompanyId(companyId));
        return success(vehicleList.stream().map(i -> new JSONObjectPro()
                .put("vehicleId", i.getId())
                .put("companyId", i.getCompanyId())
                .put("numberPlate", i.getNumberPlate())
                .put("vehicleColor", i.getVehicleColor())
                .put("vehicleModel", i.getVehicleModel())
                .put("vehiclePic", i.getVehiclePic())
                .put("objIcon", i.getObjIcon())
                .put("objType", i.getObjType())
                .put("ageVehicle", i.getAge())
        ).collect(Collectors.toList()));
    }

    @ApiOperation("监管对象名称重复验证")
    @PostMapping("/existNumberPlate")
    public AjaxResult existNumberPlate(NumberPlateExistParam param) {
        vehicleManagementService.checkExistNumberPlate(param);
        return success();
    }

    @ApiOperation("设备详情")
    @PostMapping(value = "/getInfo")
    public AjaxResult getInfo(@RequestParam Long equipmentId) {
        EquipmentInfo equipmentInfo = equipmentManagementService.selectOne(equipmentId);
        return success(equipmentInfo.toAppModel());
    }

    @ApiOperation("负责人下拉列表")
    @PostMapping("/driverSelectList")
    public AjaxResult driverSelectList(@RequestParam String name, @RequestParam Long companyId) {
        List<DriverInfo> driverList = driverInfoService.selectList(new DriverInfo().setName(name).setCompanyId(companyId));
        return success(driverList.stream().map(i -> new JSONObjectPro()
                .put("driverId", i.getId())
                .put("companyId", i.getCompanyId())
                .put("name", i.getName())
                .put("contactNumber", i.getContactNumber())
                .put("sex", i.getSex())
                .put("age", i.getAge())
                .put("idCard", i.getIdCard())
                .put("driverPic", i.getPicture())
        ).collect(Collectors.toList()));
    }

    @ApiOperation("监管对象图片上传")
    @Log(title = "FTP文件上传", businessType = BusinessType.INSERT)
    @PostMapping("/uploadVehicle")
    public AjaxResult uploadVehicle(@RequestBody MultipartFile[] file) {
        return success(uploadFile(file, "object"));
    }

    @ApiOperation("负责人图片上传")
    @Log(title = "FTP文件上传", businessType = BusinessType.INSERT)
    @PostMapping("/uploadDriver")
    public AjaxResult uploadDriver(@RequestBody MultipartFile[] file) {
        return success(uploadFileFTP(file, ftpConfig.getDriverPath(), true));
    }

    /**
     * 激活设备
     */
    @ApiOperation("保存(激活)")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/save")
    public AjaxResult activateDevice(@RequestBody EquipmentInfo equipmentInfo) {
        appEquipmentManagementService.activateDevice(equipmentInfo);
        return success();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody EquipmentInfo equipmentInfo) {
        equipmentManagementService.updateEquipment(equipmentInfo);
        return success();
    }

    @ApiOperation("删除")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(Long equipmentId) {
        equipmentManagementService.deleteEquipment(new Long[]{equipmentId});
        return success();
    }
}
